package com.liugl.listtest

import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_custom_list.*
import utils.L

class CustomList : BasicActivity() {
    private val fruitList = ArrayList<Fruit>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom_list)

        initFruit()

        val adapter = FruitAdapter(this, R.layout.fruit_item, fruitList)
        cl.adapter = adapter

        cl.setOnItemClickListener { _, _, position, _ ->
            val fruit = fruitList[position]
            Toast.makeText(this, fruit.name, Toast.LENGTH_SHORT).show()
            L.d("点击第${position}个选项")
        }
    }

    private fun initFruit() {
        L.d("初始化水果列表")

        val names = listOf(
            "Apple",
            "Banana",
            "Orange",
            "Watermelon",
            "Pear",
            "Grape",
            "Pineapple",
            "Strawberry",
            "Cherry",
            "Mango",
        )

        val images = listOf(
            R.drawable.apple_pic,
            R.drawable.banana_pic,
            R.drawable.orange_pic,
            R.drawable.watermelon_pic,
            R.drawable.pear_pic,
            R.drawable.grape_pic,
            R.drawable.pineapple_pic,
            R.drawable.strawberry_pic,
            R.drawable.cherry_pic,
            R.drawable.mango_pic,
        )

        repeat(10) {
//            for (i in 0..names.size) {
//
//            }
            fruitList.add(Fruit(names[0], images[0]))
            fruitList.add(Fruit(names[1], images[1]))
            fruitList.add(Fruit(names[2], images[2]))
            fruitList.add(Fruit(names[3], images[3]))
            fruitList.add(Fruit(names[4], images[4]))
            fruitList.add(Fruit(names[5], images[5]))
            fruitList.add(Fruit(names[6], images[6]))
            fruitList.add(Fruit(names[7], images[7]))
            fruitList.add(Fruit(names[8], images[8]))
            fruitList.add(Fruit(names[9], images[9]))
        }
    }
}